#include <qfile.h>
#include <qstringlist.h>
#include <qfileinfo.h>
#include <qtextstream.h>
#include <xwritelogexport.h>
#include <xinfomatremoteproxy.h>   //define enterMenuMode exitMenuMode
#include "infomatcommon.h"
#include "xlayout1.h"
#include "xhtmlplayer.h"
#include "xmovieplayer.h"

XLayout1::XLayout1(QObject* parent)  :QObject(parent),m_event(ENoEvent), m_htmlRoutineIndex(0), m_htmlPluIndex(0),m_movieRoutineIndex(0),m_moviePluIndex(0),m_isHtmlIdle(true),m_isCPlayerIdle(true),m_isNextPluHtml(false),m_isNextPluMovie(false), m_isPlayingMoviePlu(false),  m_initHtmlWithFullScreen(false),m_initMovieWithFullScreen(false),m_needSwitchToParitalScreen(false), m_currentPlu(-1)
{/*{{{*/
	m_xHtmlPlayer = new XHtmlPlayer();
	m_xMoviePlayer = new XMoviePlayer();
	//XDataBootConf xbootConf;
	if (EScreenModeOn==m_xbootconf.screenMode()) {
//		LOG_E("INIT With FULL SCREEN");
		m_screenMode = EPartialScreen;
		m_initHtmlWithFullScreen = false;
		m_initMovieWithFullScreen = false;
		m_xMoviePlayer->setPlayerGeometry(0,165,600,435);
	} else {
//		LOG_E("INIT W/O FULL SCREEN");
		m_screenMode=EPartialScreen;
		m_initHtmlWithFullScreen = false;
		m_initMovieWithFullScreen = false;
		m_xMoviePlayer->setPlayerGeometry(0,165,600,435);
	}
	/* ----Not Start Play Yet ------ */
}/* }}} */
XLayout1::~XLayout1() /* {{{ */
{
	if ( NULL!=m_xMoviePlayer) {
		delete m_xMoviePlayer;
		m_xMoviePlayer = NULL;
	}
	if (NULL!=m_xHtmlPlayer) {
		delete m_xHtmlPlayer;
		m_xHtmlPlayer = NULL;
	}
}/* }}} */
/* ===============================================================
 *          PUBLIC
 * ===============================================================*/
int XLayout1::doRoutine(int screenMode) /* {{{ */
{
	// (1) Partial Screen Routine (2) Full Screen Routine
	// The Case: ScreenSaver --> PLU (invalid) --> doRoutine(Switch)
	//           ScreenSaver --> Routine (partial)
	//           Screen Mode Off --> Routine (partial)
	//LOG_E("Screen Mode:%02d", screenMode);
	if (screenMode == EPartialScreen) {
		m_needSwitchToParitalScreen = true;
	} else {
		m_needSwitchToParitalScreen = false;
	}
	if (0!=startHtmlRoutinePlay(screenMode)) { LOG_E("Error.Routine.Html.Failed"); return -1;}
	doMovieRoutinePlay();
	return 0;
}/* }}} */

int XLayout1::doHalt() /* {{{ */
{
	m_xMoviePlayer->stopPlay();
	m_xHtmlPlayer->stopPlay();
	clearMovieRoutinePlayConnections();
	clearMoviePluPlayConnections();
	clearHtmlPluPlayConnections();
	clearHtmlRoutinePlayConnections();
	m_xHtmlPlayer->hide();
	return 0;
}/* }}} */

int XLayout1::doResume() /* {{{ */
{
	//XDataBootConf xbootConf;
	if (EScreenModeOn==m_xbootconf.screenMode()) {
		m_initHtmlWithFullScreen = true;
		m_initMovieWithFullScreen = true;
		doRoutine(EFullScreen);
	} else {
		m_initHtmlWithFullScreen = false;
		m_initMovieWithFullScreen = false;
		doRoutine(EPartialScreen);
	}

	return 0;
}/* }}} */

int XLayout1::doPlu (const int plu, bool needSwitchPartial)  /*{{{ */
{	
	m_htmlPluList.clear();
	m_moviePluList.clear();
	if (plu == m_currentPlu) { LOG_E("[Return] Same Plu:%d", plu); return -1;}
	bool isPassFile = false;
	if(0!=isPluValid(plu)) {
		LOG_E("[Warning][PLU File][Not Valid]");
		return -1;
	}
	isPassFile = true;
	if (isPassFile) {  //(both) valid, html valid only, movie valid only.
		m_needSwitchToParitalScreen = needSwitchPartial;   //ScreenSaver -> Plu
		// ------------ PROCESS HTML PLU -------------------------
//		if (m_needSwitchToParitalScreen) {  // no matter HTML valid or invalid, it must switch to partial
//			LOG_E("need to switch parital");
			m_xHtmlPlayer->setPlayerGeometry(600,165,200,435); 
//		}
		if (m_isNextPluHtml) {  //Html Valid
			if (0==m_xHtmlPlayer->stopPlay()) {
				m_isHtmlIdle = true;
			}
			m_htmlPluList = m_nextHtmlPluList;
			startHtmlPlu();
		}else{
			if (0==m_xHtmlPlayer->stopPlay()) {
				m_isHtmlIdle = true;
			}
			startHtmlRoutinePlay(EPartialScreen);
		}
		// -------------- PROCESS MOVIE PLU -----------------------
		if (m_isNextPluMovie) { 
			m_isCPlayerIdle = false;
			connect (m_xMoviePlayer, SIGNAL(sgCPlayerIdle()), this, SLOT(onCPlayerIdle()));
			m_xMoviePlayer->stopPlay();   
		}else {
			if (needSwitchPartial == true) {
				LOG_E("Movie Plu Not Valid: partial screen routine");
				m_isCPlayerIdle = false;
				connect (m_xMoviePlayer, SIGNAL(sgCPlayerIdle()), this, SLOT(onCPlayerIdle()));
				m_xMoviePlayer->stopPlay();   
				m_event = ERoutineWithPartialSceen;
			}
		}
	}
	return 0;
}/* }}} */

int XLayout1::doEnterScreenSaver() /* {{{ */
{
	m_event = EEnterScreenSaver;
	m_currentPlu = -1; //reset plu
	m_isCPlayerIdle = false;
	startHtmlRoutinePlay(EFullScreen);
	connect (m_xMoviePlayer, SIGNAL(sgCPlayerIdle()), this, SLOT(onCPlayerIdle()));
	m_xMoviePlayer->stopPlay();
	return 0;
}/* }}} */

int XLayout1::doEnterMenuMode()
{
	/* {{{ */
	doEnterScreenSaver();
	m_event = EEnterMenuMode;
	return 0;
	/* }}} */
}
int XLayout1::doExitMenuMode()
{
	/* {{{ */
	doExitScreenSaver();
	m_event = EExitMenuMode;
	return 0;
	/* }}} */
}
int XLayout1::doExitScreenSaver()
{/* {{{ */
	startHtmlRoutinePlay(EPartialScreen);
	m_event = EExitScreenSaver;
	m_needSwitchToParitalScreen = true;
	m_isCPlayerIdle = false;
	connect (m_xMoviePlayer, SIGNAL(sgCPlayerIdle()), this, SLOT(onCPlayerIdle()));
	m_xMoviePlayer->stopPlay();
	return 0;
	/* }}} */
}
/* ********************************************************************
 *        PROTECTED
 * ********************************************************************/
void XLayout1::processMovieEvents() /* {{{ */
{
	bool isSetPartial = false;
	if (m_isCPlayerIdle) 
	{
		// --------------- First Run Setting-------------------------
		if (m_initMovieWithFullScreen) {
			LOG_E("Movie.Init.Screen.Full");
			m_xMoviePlayer->setPlayerGeometry(0,0,600,600);
			m_initMovieWithFullScreen = false;
		} 

		// ------------ PLU Setting --------------------------------
		if (m_isNextPluMovie) {
			m_isNextPluMovie = false; //reset
			if (m_event == EEnterScreenSaver) {
				LOG_E("EneterScreenSaver --> PLU");
				m_xMoviePlayer->setPlayerGeometry(0,165,600,435);
			}
			m_moviePluList = m_nextMoviePluList;
			m_currentPlu = m_moviePluList[0].serial;   
			m_isPlayingMoviePlu = true;
			m_event = EPlu; 
		} 

		//------------- Set Geometry -----------------------------
		LOG_E("m_needSwitchToParitalScreen:%02d", m_needSwitchToParitalScreen);
		if (m_needSwitchToParitalScreen) {  //PLU and Routine
			isSetPartial = true;
			LOG_E("Need.Switch.To.Partial.Screen");
			m_xMoviePlayer->setPlayerGeometry(0,165,600,435);  //Exception Case: fast Double Press PLU#,html only 
			m_needSwitchToParitalScreen = false;
			LOG_E("do switching repaint weight mesaure");
			emit sgWMRepaint();
		}
		//------------- Start Process Event ----------------------
		switch (m_event) 
		{
		case ERoutine:
			LOG_E("[Event:Routine]");
			m_event = ENoEvent;
			startMovieRoutinePlay();
			break;
		case ERoutineWithPartialSceen:  //Exception Case: fast Double Press PLU#,html only <<Add
			LOG_E("[Event:Routine with partial screen]");
			m_event = ENoEvent;
			if (EScreenModeOn == m_xbootconf.screenMode()) {
				m_xMoviePlayer->setPlayerGeometry(0,165,600,435); //exception case:Double Press PLU#
			}
			startMovieRoutinePlay();
			emit sgWMRepaint();  //bug21279, double press html plu, crushed wm
			break;
		case EPlu:
			LOG_E("[Event:Plu]");
			if (EScreenModeOn == m_xbootconf.screenMode()) {
				if (!isSetPartial) {
					LOG_E("Switch to Partial Screen");
					m_xMoviePlayer->setPlayerGeometry(0,165,600,435); //exception case:Double Press PLU#
					LOG_E("do switching repaint weight mesaure");
					emit sgWMRepaint();
				}
			}
			m_event = ENoEvent;
			startMoviePluPlay();
			break;
		case EEnterScreenSaver:
		case EEnterMenuMode:
			LOG_E("[Event:EnterScreenSaver/EnterMenuMode");
			//Reset PLU coz it would be interrupted
			m_currentPlu = -1;
			m_isPlayingMoviePlu = false;
			m_xMoviePlayer->setPlayerGeometry(0,0,600,600);
			startMovieRoutinePlay();
			break;
		case EExitScreenSaver:
		case EExitMenuMode:
			LOG_E("[Event:ExitScreenSaver/ExitMenuMode");
			m_xMoviePlayer->setPlayerGeometry(0,165,600,435);
			startMovieRoutinePlay();
			emit sgWMRepaint();
			break;
		}
		m_event = ENoEvent;
	}
	/* }}} */
}
int XLayout1::startHtmlRoutinePlay(int mode) /* {{{ */
{  // mode: FullScreen / Parital Screen  	
	bool isPassFile = false;
	if (0==updateRoutineList()) {
		isPassFile =true;
	} else {
		LOG_E("[Error] [File Invalid]");
		return -1;
	}
	if (isPassFile) { //stop players
		if (0!=m_xHtmlPlayer->stopPlay()) { return -1;};
		m_isHtmlIdle = true;
		if (mode == EPartialScreen) {
			m_xHtmlPlayer->setPlayerGeometry(600,165,200,435);
		} else if (mode == EFullScreen) {
			m_xHtmlPlayer->setPlayerGeometry(600,0,200,600);
		}
		if (m_initHtmlWithFullScreen) {
			LOG_E("Init with Full Screen :%02d", m_initHtmlWithFullScreen);
			m_xHtmlPlayer->setPlayerGeometry(600,0,200,600);
			m_initHtmlWithFullScreen=false;
		}
		clearHtmlRoutinePlayConnections();
//		clearHtmlPluPlayConnections();
		connect (this, SIGNAL(sgUpdateHtmlList(int)), this, SLOT(onPlayHtmlRoutineDone(int)));
		emit sgUpdateHtmlList(EUpdateRoutineList);
	}
	return 0;
}/* }}} */
int XLayout1::doMovieRoutinePlay()  /* {{{ */
{	
	LOG_E("Do.Movie.Routine.Play");
	bool isPassFile = false;
	if (0==updateRoutineList()) {
		isPassFile = true;
	} else {
		LOG_E("Error.File Invalid");
		return -1;
	}
	if (isPassFile) {
		m_isCPlayerIdle = false;
		m_event = ERoutine;
		clearMovieRoutinePlayConnections();
		clearMoviePluPlayConnections();
		connect (m_xMoviePlayer, SIGNAL(sgCPlayerIdle()), this, SLOT(onCPlayerIdle()));
		m_xMoviePlayer->stopPlay();
	}
	return 0;
}/* }}} */
void XLayout1::startHtmlPlu()
{
	/* {{{ */
	if (0 == m_htmlPluList.size()) {
		LOG_E("Warning.No.html.plu");
		return;
	}
	LOG_E("Show Html Plu: %02d", m_htmlPluList[0].serial);
	clearHtmlRoutinePlayConnections();
	clearHtmlPluPlayConnections();
	connect (m_xHtmlPlayer, SIGNAL(sgPlayHtmlDone(int)), this, SLOT(onPlayHtmlPluDone(int)));
	m_xHtmlPlayer->playHtml(m_htmlPluList[0].file.toStdString().c_str(), m_htmlPluList[0].timeout, 0);
	/* }}} */
}
void XLayout1::startMoviePluPlay()   /* {{{ */
{
	if (0==m_moviePluList.size()) {
		LOG_E("Warning.No.Movie.PLU");
		return ;
	}
//	LOG_E("Current Plu: %02d", m_currentPlu);
	clearMoviePluPlayConnections();
	clearMovieRoutinePlayConnections();
	switch (m_moviePluList[0].type) 
	{
	case EPicture:
		connect (m_xMoviePlayer,SIGNAL(sgPlayMovieDone(int)), this, SLOT(onPlayMoviePluDone(int)));
		m_isPlayingMoviePlu = false;
		m_currentPlu = -1;
		m_xMoviePlayer->playMovie(m_moviePluList[0].file.toStdString().c_str(), m_moviePluList[0].timeout, 0);
		return;
	case EVideo:
		connect (m_xMoviePlayer,SIGNAL(sgPlayMovieDone(int)), this, SLOT(onPlayMoviePluDone(int)));
		m_xMoviePlayer->playMovie(m_moviePluList[0].file.toStdString().c_str(),0);
		return;
	default:
		return;
	}
}/* }}} */
int XLayout1::isPluValid(const int plu)
{/* {{{{ */
	m_isNextPluMovie = false;
	m_isNextPluHtml = false;

	if (0!=buildPluList(plu)) {
		LOG_E("Error.Plu.Invalid");
		return -1;
	};
	if (m_nextHtmlPluList.size()==0) {
//		LOG_E("[PLU] [HTML] [INVALID]");
	} else { 
		m_isNextPluHtml = true;
//		LOG_E("[PLU:%02d] [HTML] [VALID]", plu);
	}

	if (m_nextMoviePluList.size() == 0) {
//		LOG_E("[PLU:%02d] [MOVIE] [INVALID]", plu);
	}else {
		if (m_isPlayingMoviePlu==true) {
			if (plu == m_currentPlu) {
				LOG_E("Return, Same PLU");
				m_isNextPluMovie = false;
				//	LOG_E("[PLU] [MOVIE] [INVALID]");
				return -1;
			}
		}
//		LOG_E("[PLU:%02d] [Movie] [VALID]", plu);
		m_isNextPluMovie = true;
	}

	if (false == m_isNextPluHtml) {
		if (false ==m_isNextPluMovie) { 
//			LOG_E("[Return][No Such PLU %02d]", plu);
			return -1;
		}
	}
	return 0;
}/* }}} */
int XLayout1::buildPluList (const int plu)
{/* {{{ */
	QFile file;
	file.setFileName(k_file_plu_spec);
	if (false == file.open(QIODevice::ReadOnly)) {
		LOG_E("[Error] Open Plu Spec Failed");
		return -1;
	}
	QTextStream tmpStream(&file);
	QString     tmpLine;
	STFileInfo 	stList;
	QString    html, movie;
	m_nextHtmlPluList.clear();
	m_nextMoviePluList.clear();
	
	while (!tmpStream.atEnd())
	{
		tmpLine = tmpStream.readLine();
		stList.serial = tmpLine.section(':',0,0).trimmed().toInt();

		if (plu == stList.serial) {
			html   = tmpLine.section(':',1,1).trimmed();
			movie  = tmpLine.section(':',2,2).trimmed();
			stList.timeout= tmpLine.section(':',3,3).trimmed().toInt();

			if(0==stList.timeout) {
				//XDataBootConf xbootconf;
				if (0<m_xbootconf.getTimeout()) {
					stList.timeout = m_xbootconf.getTimeout();
				} else {
					stList.timeout = k_default_timeout;
				}
			}

			if(!html.isEmpty()) {
				stList.file = QString(k_html_path) + html;
				if (isFileExist(stList.file.toStdString().c_str())) {
					stList.type = EHtml;
					m_nextHtmlPluList.push_back(stList);
				} 
			}

			if(!movie.isEmpty()) {
				stList.file = QString(k_movie_path) + movie;
				if (isFileExist(stList.file.toStdString().c_str())) {
					if (isPicture(movie.toStdString().c_str())) {
						stList.type = EPicture;
					} else {
						stList.type = EVideo;
					}
					m_nextMoviePluList.push_back(stList);
				} 
			}
		}
	}
	bool isMoviePluValid = false;
	bool isHtmlPluValid = false;
	if (m_nextMoviePluList.size() ==0) {
		LOG_E("Warning.Temp Movie Plu list. Empty");
		isMoviePluValid = false;
	} else {
		isMoviePluValid = true;
	}

	if (m_nextHtmlPluList.size() ==0) {
		LOG_E("Warning.Temp Html Plu.Empty");
		isHtmlPluValid = false;
	} else {
		isHtmlPluValid = true;
	}

	if ( isMoviePluValid == false && isHtmlPluValid == false) {
		LOG_E("Warning.No PLU Valid");
		return -1;
	}

	file.close();
	return 0;
}/* }}} */
int XLayout1::updateRoutineList ()   /*  including Html and Movie Respectly */  /* {{{ */
{
	QDateTime routineListLastModified = QFileInfo(QString(k_file_playlist)).lastModified();
	if (routineListLastModified == m_routineListLastModified) { return 0; }
	LOG_E("[Changed] New Routine List");
	m_routineListLastModified = routineListLastModified;
	if (0 != buildRoutineList()) {
		LOG_E("Build Routine List Error");
		return -1;
	}
	return 0;
}/* }}} */
int XLayout1::buildRoutineList()  /* {{{ */
{   
	QFile file;
	file.setFileName (k_file_playlist);
	if (false == file.open(QIODevice::ReadOnly)) {  //TODO:Risk, if file open failed
		LOG_E("[Read Error]");
		return -1;
	}
	QTextStream tmpStream(&file);
	QString     tmpLine;
	STFileInfo 	stList;
	QString     html, movie;
	m_htmlRoutineList.clear();
	m_movieRoutineList.clear();
	while (!tmpStream.atEnd())
	{
		tmpLine = tmpStream.readLine();
		stList.serial = tmpLine.section(':',0,0).trimmed().toInt();
		html   = tmpLine.section(':',1,1).trimmed();
		movie  = tmpLine.section(':',2,2).trimmed();
		stList.timeout= tmpLine.section(':',3,3).trimmed().toInt();
		if(0==stList.timeout) {
			//XDataBootConf xbootconf;
			if (0<m_xbootconf.getTimeout()) {
				stList.timeout = m_xbootconf.getTimeout();
			} else {
				stList.timeout = k_default_timeout;
			}
		}
		if(!html.isEmpty()) {  // Build html routine list
			stList.file = QString(k_html_path) + html;
			if (isFileExist(stList.file.toStdString().c_str())) {  //Exception (1)
				stList.type = EHtml;
				m_htmlRoutineList.push_back(stList);
			}
		}
		if(!movie.isEmpty()) {  // build movie routine list
			stList.file = QString(k_movie_path) + movie;
			if (isFileExist(stList.file.toStdString().c_str())) {   // Exception (1)
				if (isPicture(movie.toStdString().c_str())) {
					stList.type = EPicture;
				} else {
					stList.type = EVideo;
				}
				m_movieRoutineList.push_back(stList);
			}
		}
	}
	if (0 == m_htmlRoutineList.size())  {
		LOG_E("Warning.Html.Routine.List.Empty.Set.teraoka.htm");
		stList.timeout = 30000;
		stList.serial  = 0;
		stList.type    = EHtml;
		stList.file    = "/usr/infomat/teraoka.htm";  //TODO:RISK.ifFileNotExisted?
		m_htmlRoutineList.push_back(stList);
	}
	if (0 == m_movieRoutineList.size())  { 
		LOG_E("Warning.Html.Routine.List.Empty.Set.teraoka.png");
		stList.timeout = 3000;
		stList.serial  = 0;
		stList.type    = EPicture;
		stList.file    = "/usr/infomat/images/teraoka.jpg"; //TODO:RISK.ifFileNotExisted?
		m_movieRoutineList.push_back(stList);
	}
	file.close();
	return 0;
}/* }}} */
void XLayout1::startMovieRoutinePlay() /* {{{ */
{
	clearMovieRoutinePlayConnections();
	clearMoviePluPlayConnections();
	connect(this,SIGNAL(sgUpdateMovieList(int)), this, SLOT(onPlayMovieRoutineDone(int)));
	emit sgUpdateMovieList(EUpdateRoutineList);
}/* }}} */
void XLayout1::clearHtmlRoutinePlayConnections()
{
	disconnect(this,SIGNAL(sgUpdateHtmlList(int)), this, SLOT(onPlayHtmlRoutineDone(int)));
	disconnect(m_xHtmlPlayer, SIGNAL(sgPlayHtmlDone(int)), this, SLOT(onPlayHtmlRoutineDone(int)));
}
void XLayout1::clearHtmlPluPlayConnections()
{
	disconnect (m_xHtmlPlayer, SIGNAL(sgPlayHtmlDone(int)), this, SLOT(onPlayHtmlPluDone(int)));
}
void XLayout1::clearMoviePluPlayConnections()
{
	disconnect (m_xMoviePlayer, SIGNAL(sgPlayMovieDone(int)), this, SLOT(onPlayMoviePluDone(int)));
}
void XLayout1::clearMovieRoutinePlayConnections()
{
	disconnect(this,SIGNAL(sgUpdateMovieList(int)), this, SLOT(onPlayMovieRoutineDone(int)));
	disconnect(m_xMoviePlayer,SIGNAL(sgPlayMovieDone(int)), this, SLOT(onPlayMovieRoutineDone(int)));
}
void XLayout1::setScreenMode(int mode)  /*  [#] */ /* {{{ */
{
	assert (NULL!=m_xHtmlPlayer);
	assert (NULL!=m_xMoviePlayer);
	switch (mode) 
	{
	case EPartialScreen:
		m_xMoviePlayer->setPlayerGeometry(0,165,600,435);
		m_xHtmlPlayer->setPlayerGeometry(600,165,200,435);
		break;
	case EFullScreen:
		m_xMoviePlayer->setPlayerGeometry(0,0,600,600);
		m_xHtmlPlayer->setPlayerGeometry(600,0,200,600);
		break;
	default:
		LOG_E("ERROR: No Such Screen Mode");
		return;
	}
}/* }}} */
void XLayout1::checkList ()
{/* {{{ */
	LOG_E("html Routine List");
	for (int i=0; i<m_htmlRoutineList.size(); ++i)
		LOG_E("index:%d, %s",i,m_htmlRoutineList[i].file.toStdString().c_str());
	LOG_E("\n\n");
	LOG_E("html Plu list");
	for (int i=0; i<m_htmlPluList.size(); ++i)
		LOG_E("index:%d, %s",i,m_htmlPluList[i].file.toStdString().c_str());
	LOG_E("\n\n");
	LOG_E("movie Routine list");
	for (int i=0; i<m_movieRoutineList.size(); ++i) 
		LOG_E("index:%d, %s",i,m_movieRoutineList[i].file.toStdString().c_str());
	LOG_E("\n\n");
	LOG_E("movie plu list");
	for (int i=0; i<m_moviePluList.size();++i) 
		LOG_E("index:%d, %s",i,m_moviePluList[i].file.toStdString().c_str());
}/* }}} */

/* ====================================================================
 *     SIGNAL
 * ====================================================================*/
void XLayout1::onCPlayerIdle() /* {{{ */
{
	LOG_E("[Pass][CPlayer][Idle]");
	disconnect (m_xMoviePlayer, SIGNAL(sgCPlayerIdle()), this, SLOT(onCPlayerIdle()));
	m_isCPlayerIdle = true;
	processMovieEvents();
}/* }}} */
void XLayout1::onPlayHtmlRoutineDone(int id) /* {{{ */
{
	LOG_E(" [ROUTINE PLAY] [HTML] ");
	if (id == EUpdateRoutineList ) {
		LOG_E("[OK][Start New Routine List]");
		disconnect (this, SIGNAL(sgUpdateHtmlList(int)), this, SLOT(onPlayHtmlRoutineDone(int)));
	} else if (id != m_htmlRoutineIndex-1) {
		LOG_E("[Reject] Id Not Match");
		return;
	} else {
		LOG_E("[PASS] Update New File");
	}
	updateRoutineList();
	if (m_htmlRoutineIndex >= m_htmlRoutineList.size()) {
		LOG_E("[Reset] [m_htmlRoutineList]");
		m_htmlRoutineIndex = 0;
	}
	clearHtmlRoutinePlayConnections();
	clearHtmlPluPlayConnections();
	
	LOG_E("Waiting ... signal ... html timer: m_playIndex:%02d", m_htmlRoutineIndex);
	connect (m_xHtmlPlayer, SIGNAL(sgPlayHtmlDone(int)), this, SLOT(onPlayHtmlRoutineDone(int)));
	m_xHtmlPlayer->playHtml(m_htmlRoutineList[m_htmlRoutineIndex].file.toStdString().c_str(), m_htmlRoutineList[m_htmlRoutineIndex].timeout, m_htmlRoutineIndex);
	
	if (m_htmlRoutineIndex != -1 ) 
		m_htmlRoutineIndex++;
}/* }}} */
void XLayout1::onPlayMovieRoutineDone(int id)  /* {{{ */
{	
	LOG_E(" [ROUTINE] [Movie] ");
	if ( id == EUpdateRoutineList ) {
		LOG_E("[OK][Movie][Start New Routine List]");
		disconnect (this, SIGNAL(sgUpdateMovieList(int)), this, SLOT(onPlayMovieRoutineDone(int)));
	} else if (id != m_movieRoutineIndex-1) {
		LOG_E("Id=%02d, Correct Id:%02d", id, m_movieRoutineIndex-1);
		LOG_E("[Reject] Id Not Match");
		return;
	}else {
		LOG_E("[PASS] Update New File");
	}
	updateRoutineList();
	if (m_movieRoutineIndex >= m_movieRoutineList.size()) {
		LOG_E("[Reset] [m_movieRoutineIndex]");
		m_movieRoutineIndex = 0;
	}
	clearMovieRoutinePlayConnections();   //necessary
	clearMoviePluPlayConnections();       //necessary
	if (0 == m_movieRoutineList.size()) {LOG_E("[Return]Movie Empty"); return;}
	switch (m_movieRoutineList[m_movieRoutineIndex].type)		
	{
	case EPicture:
		LOG_E("Waiting ... timer ... Picture Timer :m_playIndex:%02d", m_movieRoutineIndex);
		connect (m_xMoviePlayer,SIGNAL(sgPlayMovieDone(int)), this, SLOT(onPlayMovieRoutineDone(int)));
		m_xMoviePlayer->playMovie(m_movieRoutineList[m_movieRoutineIndex].file.toStdString().c_str(), m_movieRoutineList[m_movieRoutineIndex].timeout, m_movieRoutineIndex);
		break;
	case EVideo:
		LOG_E("Waiting ... signal ... Video PlayEnd : m_playIndex:%02d", m_movieRoutineIndex);
		connect (m_xMoviePlayer,SIGNAL(sgPlayMovieDone(int)), this, SLOT(onPlayMovieRoutineDone(int)));
		m_xMoviePlayer->playMovie(m_movieRoutineList[m_movieRoutineIndex].file.toStdString().c_str(), m_movieRoutineIndex);
		break;
	default:
		LOG_E("[Error] Not Movie File");
		return;
	}
	m_movieRoutineIndex++;
}/* }}} */

void XLayout1::onPlayHtmlPluDone(int id)/* {{{ */
{ 	LOG_E("===== HTML.PLU.FINISH=======");
	startHtmlRoutinePlay(EPartialScreen);
} /* }}} */

void XLayout1::onPlayMoviePluDone(int id )/* {{{ */
{ 	LOG_E("===== Movie.Plu.Finish =====");
	m_isPlayingMoviePlu = false;
	m_currentPlu = -1;
	startMovieRoutinePlay();
} /* }}} */
